package com.android.SchoolManagerSystem;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.SchoolManagerSystem.action.GetCurriculaInfo;
import com.android.SchoolManagerSystem.action.GetTranScript;
import com.android.SchoolManagerSystem.action.UserLogin;
import com.android.SchoolManagerSystem.data.Userinfo;

public class SchoolManagerSystem extends Activity implements
		View.OnClickListener {
	OnlineCoursePage mOnlineCoursePage = null;
	Userinfo mCurUserinfo = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ViewGroup rootView = (ViewGroup) findViewById(R.id.operaterlist);
		LayoutInflater inflater = getLayoutInflater();
		inflater.inflate(R.layout.mycenter, rootView);
		inflater.inflate(R.layout.scorequery, rootView);
		inflater.inflate(R.layout.onlinecourse, rootView);
		inflater.inflate(R.layout.login, rootView);

		initOnlineCoursePage();

		// operate list page
		findViewById(R.id.operaterlist_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.operaterlist_btn_mycenter).setOnClickListener(this);
		findViewById(R.id.operaterlist_btn_scorequery).setOnClickListener(this);
		findViewById(R.id.operaterlist_btn_onlinecourse).setOnClickListener(
				this);
		findViewById(R.id.operaterlist_btn_practive).setOnClickListener(this);
		findViewById(R.id.operaterlist_btn_jobinfo).setOnClickListener(this);
		findViewById(R.id.operaterlist_btn_stationmail)
				.setOnClickListener(this);
		// mycenter page
		findViewById(R.id.mycenter_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.mycenter_btn_ok).setOnClickListener(this);
		findViewById(R.id.mycenter_btn_cancel).setOnClickListener(this);
		// score query page
		findViewById(R.id.scorequery_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.scorequery_btn_ok).setOnClickListener(this);
		// online course page
		findViewById(R.id.onlinecourse_btn_backtomain).setOnClickListener(this);
		findViewById(R.id.onlinecourse_btn_ok).setOnClickListener(this);

		// login page
		findViewById(R.id.login_btn_login).setOnClickListener(this);
		findViewById(R.id.login_btn_reg).setOnClickListener(this);

		PageManager.setRootView(rootView);
		// PageManager.changeToRootPage();
		PageManager.changeToPage(PageManager.PAGE_LOGIN);
	}

	private void initOnlineCoursePage() {
		mOnlineCoursePage = new OnlineCoursePage(this,
				findViewById(R.id.onlinecourse));

		List<String> courseNameList = new ArrayList<String>();
		courseNameList.add("AA");
		courseNameList.add("BB");
		courseNameList.add("CC");

		mOnlineCoursePage.resetOnlineCourseList(courseNameList);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		// BEGIN : ryan_hsueh@asus.com - add : control UI components to hide
		case KeyEvent.KEYCODE_BACK:
			if (!PageManager.isRootPage()) {
				PageManager.changeToRootPage();
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		// operate list page
		case R.id.operaterlist_btn_backtomain: {
			if (PageManager.isRootPage()) {
				mCurUserinfo.clear();	
				mCurUserinfo = null;
				PageManager.changeToPage(PageManager.PAGE_LOGIN);
			} else
				PageManager.changeToRootPage();
			break;
		}

		case R.id.operaterlist_btn_mycenter: {
			PageManager.changeToPage(PageManager.PAGE_MYCENTER);
			String myinfo = "姓名：" + mCurUserinfo.strusername + "\n学号："
					+ mCurUserinfo.struserid + "\n班级： " + mCurUserinfo.strClass;
			TextView mtv = (TextView) findViewById(R.id.mycenter_text_mycenter);
			mtv.setText(myinfo);
			break;
		}

		case R.id.operaterlist_btn_scorequery: {
			if (mCurUserinfo.struserid == null) {
				break;
			} else {
				PageManager.changeToPage(PageManager.PAGE_SCOREQUERY);
				break;
			}
		}

		case R.id.operaterlist_btn_onlinecourse: {
			GetCurriculaInfo getOnlinecoure = new GetCurriculaInfo(mCurUserinfo);
				if(getOnlinecoure.go(GetCurriculaInfo.OPER_QUERY)){
				/*显示查询到的课程,数据来自mCurUserinfo.MyCurricula的strCurriculaName，bChoosed*/
				}
			PageManager.changeToPage(PageManager.PAGE_ONLINECOURSE);
			break;
		}

		case R.id.operaterlist_btn_practive: {
			PageManager.changeToPage(PageManager.PAGE_PRACTIVE);
			break;
		}

		case R.id.operaterlist_btn_jobinfo: {
			PageManager.changeToPage(PageManager.PAGE_JOBINFO);
			break;
		}

		case R.id.operaterlist_btn_stationmail: {
			PageManager.changeToPage(PageManager.PAGE_STATIONMAIL);
			break;
		}

			// mycenter page
		case R.id.mycenter_btn_backtomain: {
			PageManager.changeToRootPage();
			break;
		}

		case R.id.mycenter_btn_ok: {
			PageManager.changeToRootPage();
			break;
		}

		case R.id.mycenter_btn_cancel: {
			PageManager.changeToRootPage();
			break;
		}

			// score query page
		case R.id.scorequery_btn_backtomain: {
			PageManager.changeToRootPage();
			break;
		}

		case R.id.scorequery_btn_ok: {
			GetTranScript getts = new GetTranScript(mCurUserinfo, "1"/*界面上的学期*/,"2011"/*界面上得到的年份*/);
			getts.go();
			/*将mCurUserinfo.MyScore list里面的每个节点下，tmpScript.strMark;tmpScript.CCula.strCurriculaName列出来*/
			break;
		}

			// online course page
		case R.id.onlinecourse_btn_backtomain: {
			PageManager.changeToRootPage();
			break;
		}
		case R.id.login_btn_login: {
			EditText txusername = (EditText) findViewById(R.id.login_userid_edit);
			EditText txuserpsw = (EditText) findViewById(R.id.login_userid_edit);
		//	mCurUserinfo = new Userinfo(txusername.getText().toString(),
					//txuserpsw.getText().toString());
			mCurUserinfo = new Userinfo("2110712009","2110712009");
			UserLogin ua = new UserLogin(mCurUserinfo);
			if (ua.login())
				PageManager.changeToRootPage();
			else {
				Toast.makeText(getApplicationContext(), "登入失败",
						Toast.LENGTH_SHORT).show();
			}
			break;
		}
		case R.id.onlinecourse_btn_ok: {
			/*将mCurUserinfo.MyCurricula里面的数据清除，将用户选择的课程名/id加入该list*/
			GetCurriculaInfo getOnlinecoure = new GetCurriculaInfo(mCurUserinfo);
			if(getOnlinecoure.go(GetCurriculaInfo.OPER_MODIFY)){
			/*显示成功*/
			}
			PageManager.changeToRootPage();
			break;
		}
		}
	}
}